package cn.edu.neusoft.cqy917.zyfypt_cqy917.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.R;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.CollectListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.ExistsListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.model.CollectModel;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.model.ExistsModel;

public class ViewProjectActivity extends AppCompatActivity {
    private int resid;
    private int userid;
    private WebView webView;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    ActionMenuItemView menuItemView;
    ActionMenuItemView menuItemView2;
    String sessionID="";
    SharedPreferences sharedPreferences;
    Context context;
    CollectListener collectListener=new CollectListener() {
        @Override
        public void oncollectResponse(String msg) {
            menuItemView=(ActionMenuItemView)findViewById(R.id.menu1);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,
                        "收藏失败",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"收藏成功",
                Toast.LENGTH_SHORT).show();
                menuItemView.setTitle("取消收藏");
            }
        }

        @Override
        public void oncollectFail(String msg) {

        }

        @Override
        public void oncancelResponse(String msg) {
            menuItemView=(ActionMenuItemView)findViewById(R.id.menu1);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,
                        "取消收藏失败",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"取消收藏成功",
                        Toast.LENGTH_SHORT).show();
                menuItemView.setTitle("收藏");
            }
        }

        @Override
        public void oncancelFail(String msg) {

        }

        @Override
        public void onjudgeResponse(String msg) {
            menuItemView=(ActionMenuItemView)findViewById(R.id.menu1);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,
                        "已收藏",Toast.LENGTH_SHORT).show();
                menuItemView.setTitle("取消收藏");
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"未收藏",
                        Toast.LENGTH_SHORT).show();
                menuItemView.setTitle("收藏");
            }
        }

        @Override
        public void onjudgeFail(String msg) {

        }
    };


    ExistsListener existsListener=new ExistsListener() {
        @Override
        public void onexistsResponse(String msg) {
            menuItemView2=(ActionMenuItemView)findViewById(R.id.menu2);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,"关注失败",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"关注成功",Toast.LENGTH_SHORT)
                        .show();
                menuItemView2.setTitle("取消关注");
            }
        }

        @Override
        public void onexistsFail(String msg) {

        }

        @Override
        public void oncancelResponse(String msg) {
            menuItemView2=(ActionMenuItemView)findViewById(R.id.menu2);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,"取消关注失败",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"取消关注成功",Toast.LENGTH_SHORT)
                        .show();
                menuItemView2.setTitle("关注");
            }
        }

        @Override
        public void oncancelFail(String msg) {

        }

        @Override
        public void onjudgeResponse(String msg) {
            menuItemView2=(ActionMenuItemView)findViewById(R.id.menu2);
            if (msg.equals("0")){
                Toast.makeText(ViewProjectActivity.this,"已关注",Toast.LENGTH_SHORT)
                        .show();
                menuItemView2.setTitle("取消关注");
            }
            else {
                Toast.makeText(ViewProjectActivity.this,"未关注",Toast.LENGTH_SHORT)
                        .show();
                menuItemView2.setTitle("关注");
            }
        }

        @Override
        public void onjudgeFail(String msg) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_project);
        System.out.println("----查看文章详情");
  context =ViewProjectActivity.this;
        sharedPreferences=getSharedPreferences("login",MODE_PRIVATE);
        readSP();

        resid  = getIntent().getIntExtra("resid",1);
        userid = getIntent().getIntExtra("userid",7);


        webView = (WebView)findViewById(R.id.webview);
        webView.loadUrl(BASEURL+"project.php/show/index/id/"+resid);


    }
    private void readSP(){
        sessionID = sharedPreferences.getString("sessionID",null);}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        CollectModel collectModel=new CollectModel();
        collectModel.judge("project",resid,sessionID,collectListener);
        ExistsModel existsModel=new ExistsModel();
        existsModel.judge("userfocus",userid,sessionID,existsListener);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        menuItemView=(ActionMenuItemView)findViewById(R.id.menu1);
        menuItemView2=(ActionMenuItemView)findViewById(R.id.menu2);
        switch (item.getItemId()){
            case  R.id.menu1:
                if (menuItemView.getText().equals("取消收藏")){
                    CollectModel collectModel=new CollectModel();
                    collectModel.uncollect("project",resid,sessionID,collectListener);
                }
                else {
                    CollectModel collectModel=new CollectModel();
                    collectModel.collect("project",resid,sessionID,collectListener);
                }
                break;
            case  R.id.menu2:
                if (menuItemView2.getText().equals("取消关注")){
                    ExistsModel existsModel=new ExistsModel();
                    existsModel.unexists("userfocus",userid,sessionID,existsListener);
                }
                else {
                    ExistsModel existsModel=new ExistsModel();
                    existsModel.exists("userfocus",userid,sessionID,existsListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
